#include "mysql_chat_session.hpp"
#include "mysql_apply.hpp"
#include "mysql_relation.hpp"

#include "uuid.hpp"

#include <gflags/gflags.h>

DEFINE_bool(log_mode, false, "程序的运行模式: true-发布模式 false-调试模式");
DEFINE_string(log_file, "", "发布模式下，制定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，制定日志的输出等级");

void relation_insert(bite_im::RelationTable &table)
{
    table.insert("用户-1", "用户-2");
    table.insert("用户-1", "用户-3");
    table.insert("用户-1", "用户-4");
    table.insert("用户-1", "用户-5");
}
void relation_remove(bite_im::RelationTable &table)
{
    table.remove("用户-1", "用户-2");
    table.remove("用户-1", "用户-3");
    table.remove("用户-1", "用户-4");
    table.remove("用户-1", "用户-5");
}

void relation_exist(bite_im::RelationTable &table)
{
    std::cout << table.exist("用户-2", "用户-1") << std::endl; // true
    std::cout << table.exist("用户-2", "用户-3") << std::endl; // false
}

void relation_friends(bite_im::RelationTable &table)
{
    auto friends_list = table.friends("用户-1"); // true
    for (auto friend_id : friends_list)
    {
        std::cout << friend_id << std::endl;
    }
}

void apply_insert(bite_im::FriendApplyTable &table)
{
    bite_im::FriendApply fa1("事件-1", "用户-2", "用户-1");
    bite_im::FriendApply fa2("事件-2", "用户-3", "用户-1");
    bite_im::FriendApply fa3("事件-3", "用户-4", "用户-1");
    table.insert(fa1);
    table.insert(fa2);
    table.insert(fa3);
}
void apply_remove(bite_im::FriendApplyTable &table)
{
    table.remove("用户-2", "用户-1");
    table.remove("用户-3", "用户-1");
    table.remove("用户-4", "用户-1");
}
void apply_exist(bite_im::FriendApplyTable &table)
{
    std::cout << table.exist("用户-2", "用户-1") << std::endl;
    std::cout << table.exist("用户-1", "用户-2") << std::endl;
}
void apply_friends(bite_im::FriendApplyTable &table)
{
    auto apply_list = table.friendApplys("用户-1"); // true
    for (auto friend_id : apply_list)
    {
        std::cout << friend_id << std::endl;
    }
}

void chat_insert(bite_im::ChatSessionTable &table)
{
    bite_im::ChatSession cs1("会话-1", "群聊-1", bite_im::ChatSessionType::GROUP);
    bite_im::ChatSession cs2("会话-2", "", bite_im::ChatSessionType::SINGLE);
    bite_im::ChatSession cs3("会话-3", "", bite_im::ChatSessionType::SINGLE);
    bite_im::ChatSession cs4("会话-4", "群聊-2", bite_im::ChatSessionType::GROUP);
    table.insert(cs1);
    table.insert(cs2);
    table.insert(cs3);
    table.insert(cs4);
}
void chat_remove(bite_im::ChatSessionTable &table)
{
    // table.remove("3b70-49c7-dfe4-0004", "5d71-92bf-2176-0003");
    // table.remove("会话-1");
    // table.remove("会话-2");
    // table.remove("会话-3");
    // table.remove("会话-4");
}

void chat_select(bite_im::ChatSessionTable &table)
{
    auto chat_session1 = table.select("会话-1");
    std::cout << "chat_session_name: " << chat_session1->chat_session_name() << std::endl;
    std::cout << "chat_session_type: " << (int)chat_session1->chat_session_type() << std::endl;
    auto chat_session2 = table.select("会话-2");
    std::cout << "chat_session_name: " << chat_session2->chat_session_name() << std::endl;
    std::cout << "chat_session_type: " << (int)chat_session2->chat_session_type() << std::endl;
    auto chat_session3 = table.select("会话-3");
    std::cout << "chat_session_name: " << chat_session3->chat_session_name() << std::endl;
    std::cout << "chat_session_type: " << (int)chat_session3->chat_session_type() << std::endl;
    auto chat_session4 = table.select("会话-4");
    std::cout << "chat_session_name: " << chat_session4->chat_session_name() << std::endl;
    std::cout << "chat_session_type: " << (int)chat_session4->chat_session_type() << std::endl;
}

void chat_single(bite_im::ChatSessionTable &table)
{
    auto single_list = table.singleChatSession("3b70-49c7-dfe4-0004");

    for (auto single : single_list)
    {
        std::cout << "----------------------------------------------" << std::endl;
        std::cout << "single_session_id: " << single.chat_session_id << std::endl;
        std::cout << "single_friend_id: " << single.friend_id << std::endl;
    }
}

void chat_group(bite_im::ChatSessionTable &table)
{
    auto group_list = table.groupChatSession("ad3a-7782-650d-0000");
    std::cout << "----------------------------------------------" << std::endl;
    for (auto group : group_list)
    {
        std::cout << "group_session_id: " << group.chat_session_id << std::endl;
        std::cout << "group_chat_session_name: " << group.chat_session_name << std::endl;
    }
}

int main(int argc, char *argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    bite_im::initLogger(FLAGS_log_mode, FLAGS_log_file, FLAGS_log_level);

    auto db = bite_im::ODBFactory::create("root", "Czh432248", "ChatRoom",
                                          "127.0.0.1", 0, "utf8", 3);

    bite_im::FriendApplyTable apply(db);
    bite_im::RelationTable relation(db);
    bite_im::ChatSessionTable chat(db);

    // relation_insert(relation);
    // std::this_thread::sleep_for(std::chrono::seconds(2));
    // relation_exist(relation);
    // relation_friends(relation);
    // std::this_thread::sleep_for(std::chrono::seconds(10));
    // relation_remove(relation);

    // apply_insert(apply);
    // apply_friends(apply);
    // apply_exist(apply);
    // std::this_thread::sleep_for(std::chrono::seconds(10));
    // apply_remove(apply);

    // chat_insert(chat);
    // chat_select(chat);
    // chat_single(chat);
    // chat_group(chat);
    // std::this_thread::sleep_for(std::chrono::seconds(10));
    // chat_remove(chat);
    return 0;
}